package db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import data.Kunde;


public class KundeDAO {
	
	private DBConnect db = DBConnect.getInstance();
	private Connection con;
	
	public KundeDAO() {
		con = db.getConnection();
	}
	
	public Kunde findKunde(String usr, String pwd) {
		Kunde kunde = this.createKunde(pwd, usr);
		return kunde;
	}
	
	
	private Kunde createKunde(String pwd, String usr) {
		Kunde kunde;
		
		String query = "SELECT * FROM kunde WHERE username LIKE ? AND passwort LIKE ?";

		try {
			PreparedStatement st = con.prepareStatement(query);
			st.setString(1, usr + "%");
			st.setString(2, pwd + "%");
			ResultSet rs = st.executeQuery();
			while(rs.next()) {
				System.out.println(rs);
				kunde = new Kunde(pwd,usr);
				
				int id = rs.getInt("id");
				String vorname = rs.getString("vorname");
				String nachname = rs.getString("nachname");
				String email = rs.getString("email");
				
				kunde.setKundenNummer(id);
				kunde.setVorname(vorname);
				kunde.setNachname(nachname);
				kunde.setEmail(email);
				
				return kunde;
				
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean storeNewKunde(Kunde newKunde) {
		if(newKunde!=null)
			return true;
		
		return false;
		//speichert Kundendaten
	}

}
